package com.hyj.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hyj.mapper.UsersMapper;
import com.hyj.entity.Fenye;
import com.hyj.entity.Roles;
import com.hyj.entity.UserRoles;
import com.hyj.entity.Users;
import com.hyj.service.RolesService;
import com.hyj.service.UserRolesService;
import com.hyj.service.UsersService;

@Controller
public class UsersController {
	@Autowired
	private UsersService usersService;
	@Autowired
	private Fenye fenye;
	@Autowired
	private RolesService rolesService;
	@Autowired
	private UserRolesService userRolesService;
	@Autowired
	private UsersMapper usersmapper;
	
	//查询用户（模糊查询）
		@RequestMapping(value = "getUserAlls", method = RequestMethod.POST)
		@ResponseBody
		public Fenye<Users> getUserAlls(Integer page, Integer rows,
				String userName, String startTime, String endTime, Integer isclose) {
			fenye.setPage((page - 1) * rows);
			fenye.setPageSize(rows);
			fenye.setUserName(userName);
			fenye.setStartTime(startTime);
			fenye.setEndTime(endTime);
			fenye.setIsclose(isclose);
			fenye = usersService.selectAllUsers(fenye);
			return fenye;
		}
		//添加用户
		@RequestMapping(value = "addAllUsers")
		@ResponseBody
		public Integer addAllUsers(Users users) {
			return usersService.addUsers(users);

		}
		//删除用户
		@RequestMapping(value = "deleteAllUsers")
		@ResponseBody
		public Integer deleteAllUsers(Integer id) {
			return usersService.deleteUsers(id);

		}
		//修改用户
		@RequestMapping(value = "updateAllUsers")
		@ResponseBody
		public Integer updateAllUsers(Users users) {
			return usersService.updateUsers(users);

		}

		/* 锁定，解锁用户 */
		@RequestMapping(value = ("OpenCloseUser"), method = RequestMethod.POST)
		@ResponseBody
		public Integer OpenCloseUser(Users users) {
			return usersService.updateYonghujiesoByisLockout(users);
		}

		// 重置密码
		@RequestMapping(value = ("ResetUserPas"), method = RequestMethod.POST)
		@ResponseBody
		public Integer ResetUserPas(Users users) {
			return usersService.updateResetpas(users);
		}

		// 根据角色查询相应权限
		@RequestMapping(value = ("getUserByRolesId"), method = RequestMethod.POST)
		@ResponseBody
		public Fenye<Users> getUserByRolesId(Integer page, Integer rows,
				Integer roleId) {
			fenye.setPage((page - 1) * rows);
			fenye.setPageSize(rows);
			return usersService.selectUserByRolesId(fenye, roleId);
		}
		//查询角色
		@RequestMapping(value = ("getRolesAll"), method = RequestMethod.POST)
		@ResponseBody
		public Fenye<Roles> getRolesAll(Integer page, Integer rows) {
			fenye.setPage(0);
			fenye.setPageSize(100);
			return rolesService.selectAllRoles(fenye);

		}
		//根据编号查询角色
		@RequestMapping(value = ("getuRoles"), method = RequestMethod.POST)
		@ResponseBody
		public List<Roles> getRoles(Integer id) {

			return rolesService.selectrolebyid(id);

		};
		//添加角色
		@RequestMapping(value = ("addUserRole"), method = RequestMethod.POST)
		@ResponseBody
		public Integer addUserRoles(UserRoles userRoles) {
			return userRolesService.addUserRole(userRoles);

		}
		//删除用户角色
		@RequestMapping(value = ("delUserRole"), method = RequestMethod.POST)
		@ResponseBody
		public Integer delUserRoles(UserRoles userRoles) {
			return userRolesService.delUserRole(userRoles);

		}
		
		//即时通讯
		@RequestMapping(value = ("selectAlluserByisNotName"), method = RequestMethod.POST)
		@ResponseBody
		public List<Users> selectAlluserByisNotName(HttpSession session){
			String loginName=(String)session.getAttribute("name");
			System.out.println(loginName);
			List<Users> selectAlluserByisNotName = usersmapper.selectAlluserByisNotName(loginName);
			return selectAlluserByisNotName;
		}
	}
